package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.CustomerInfo;
import com.ruoyi.system.domain.StandardScript;
import com.ruoyi.system.mapper.StandardScriptMapper;
import com.ruoyi.system.service.IStandardScriptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 标准话术Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class StandardScriptServiceImpl implements IStandardScriptService {

    private static final Logger log = LoggerFactory.getLogger(StandardScriptServiceImpl.class);


    @Autowired(required = false)
    private StandardScriptMapper standardScriptMapper;

    /**
     * 查询标准话术
     *
     * @param id 标准话术主键
     * @return 标准话术
     */
    @Override
    public StandardScript selectStandardScriptById(String id) {
        return standardScriptMapper.selectStandardScriptById(id);
    }

    /**
     * 查询标准话术列表
     *
     * @param standardScript 标准话术
     * @return 标准话术
     */
    @Override
    public List<StandardScript> selectStandardScriptList(StandardScript standardScript) {
        return standardScriptMapper.selectStandardScriptList(standardScript);
    }

    /**
     * 新增标准话术
     *
     * @param standardScript 标准话术
     * @return 结果
     */
    @Override
    public int insertStandardScript(StandardScript standardScript) {
        standardScript.setCreateTime(DateUtils.getNowDate());
        return standardScriptMapper.insertStandardScript(standardScript);
    }

    /**
     * 修改标准话术
     *
     * @param standardScript 标准话术
     * @return 结果
     */
    @Override
    public int updateStandardScript(StandardScript standardScript) {
        standardScript.setUpdateTime(DateUtils.getNowDate());
        return standardScriptMapper.updateStandardScript(standardScript);
    }

    /**
     * 批量删除标准话术
     *
     * @param ids 需要删除的标准话术主键
     * @return 结果
     */
    @Override
    public int deleteStandardScriptByIds(Long[] ids) {
        return standardScriptMapper.deleteStandardScriptByIds(ids);
    }

    /**
     * 删除标准话术信息
     *
     * @param id 标准话术主键
     * @return 结果
     */
    @Override
    public int deleteStandardScriptById(Long id) {
        return standardScriptMapper.deleteStandardScriptById(id);
    }


    /**
     * 导入用户数据
     *
     * @param userList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<StandardScript> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (StandardScript user : userList) {
            try {
                // 验证是否存在这个用户
                List<StandardScript> list = standardScriptMapper.selectStandardScriptList(user);
                if (list == null || list.isEmpty()) {
                    standardScriptMapper.insertStandardScript(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getCustomerReply() + " 导入成功");
                } else if (isUpdateSupport) {
                    user = list.get(0);
                    standardScriptMapper.updateStandardScript(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getCustomerReply() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getCustomerReply() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getCustomerReply() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
